package com.learning.java.optimize.design.pattern.strategy;

import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;

/**
 * ClassName: FeeCalculationService
 * Description: 收银服务设计
 * Date: 2020/3/22 17:11 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
@Service
public class FeeCalculationServiceDesign {

    @Autowired
    ApplicationContext context;


    /**
     * 收银方法
     *
     * @param type 会员类型
     * @param fee  费用
     * @return
     */
    public double calculation(String type, double fee) {
        Map<String, DiscountStrategy> matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(context, DiscountStrategy.class, true, false);
        if (!matchingBeans.isEmpty()) {
            ArrayList<DiscountStrategy> discountStrategies = new ArrayList<>(matchingBeans.values());
            for (DiscountStrategy discountStrategy : discountStrategies) {
                if (discountStrategy.supportType(type)) {
                    return discountStrategy.discount(fee);
                }
            }
        }
        return fee;
    }
}
